tic;
%%% produces images with the different settings


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for id=[3 4];
for iniG=[1 2 3]
for lim_angl=[160 35] ;

for noise=[0 .03];
for mode=[0];
method='gradient';
image='ball';
maxit=1000;
normalize_bool=true;
cut=false;

switch iniG
    case 1
        initialGuessStr='sfbp';
    case 2
        initialGuessStr='fbp';
    case 3
        initialGuessStr='zero';
end
constThreshValue=1;
imgSize=512;
save_folder='./recImg/';


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


angles=(-lim_angl/2):(lim_angl/2);
truncate=true;


switch mode
    case 0
        mode='';
    case 1
        mode='smooth_tst_';
end

switch id
    case 1
        identifier=['limitedAngle_outerFading_' num2str(lim_angl)];
    case 2
        identifier=['limitedAngle_innerFading_' num2str(lim_angl)];
    case 3
        identifier=['limitedAngle_fixedFading_3_' num2str(2/3*lim_angl) '_' num2str(lim_angl)];
    case 4
        identifier=['limitedAngle_fixedFading_5_' num2str(lim_angl) '_' num2str(1.1*lim_angl)];

end

if(~strcmp(mode,''))
    identifier={identifier,[1 1],[mode num2str(2/3*lim_angl) '_' num2str(7/6*lim_angl)]};      
end

switch image
    case 'phantom'
        img=phantom(imgSize);
    case 'lena'
        img=double(imread('lena512.pgm'))/255;
        sd=512/imgSize;
        if(sd>1)
            img=sample_down(img,sd);
        end
    case 'ball'
        img=get_image('ball',[imgSize,imgSize],'mid','srp30');
end

Y=radon(img,angles);

if(noise>0)
    Y=addNoise(Y,noise);
end


switch method
    case 'gradient'
        out1=my_iterative_soft_thresholding(Y,angles,imgSize,maxit,initialGuessStr,constThreshValue,identifier);
    case 'cg'
        out1=my_iterative_cg(Y,angles,imgSize,maxit,initialGuessStr,constThreshValue,identifier);
    case 'cgV2'
        out1=my_iterative_cgV2(Y,angles,imgSize,maxit,initialGuessStr,constThreshValue,identifier);
    case 'cgV3'
        out1=my_iterative_cgV3(Y,angles,imgSize,maxit,initialGuessStr,constThreshValue,identifier);
    case 'newton'
        out1=my_iterative_newton(Y,angles,imgSize,maxit,initialGuessStr,constThreshValue,identifier);
    case 'lp_BB'
        out1=my_lp_BB(Y,angles,imgSize,maxit,initialGuessStr,constThreshValue,identifier);
end
min_x=find(out1.error==min(out1.error));

if(iscell(identifier))
   identifier=identifier{1};
end


if(normalize_bool)
    for i=1:length(out1.error)
       out1.iterates(:,:,i)= normalize(out1.iterates(:,:,i),1,cut);
    end
end


%imwrite(out1.iterates(:,:,min_x),['./recImg/' image '_' identifier '_' initialGuessStr '_noi' num2str(noise) '_it' num2str(min_x)  '.png'],'PNG');%['./recImg/' image '_' identifier '_' initialGuessStr '_noi' num2str(noise) 'it' min_x  '.png'],'PNG');


success=0;
tries=0;
while(~success && tries<100)
   try 
       tries=tries+1;
imwrite(out1.iterates(:,:,1),[save_folder image '_' identifier '_' initialGuessStr '_' method '_normal' num2str(normalize_bool) '_noi' num2str(noise) '_' mode  '.tif'],'tif','writemode','overwrite');
    success=1;
    fprintf('.'); 
    catch err
        fprintf(['err ' num2str(tries)]); 
        pause(eps);
        if(tries>=99)
            throw(err);
        end
    end
end
for i=2:length(out1.error)
    success=0;
    tries=0;
    while(~success && tries<100)
        try
        tries=tries+1;
        imwrite(out1.iterates(:,:,i),[save_folder image '_' identifier '_' initialGuessStr '_' method '_normal' num2str(normalize_bool) '_noi' num2str(noise) '_' mode  '.tif'],'tif','writemode','append');
        
        success=1;
        fprintf('.'); 
        catch err
            fprintf(['err ' num2str(tries)]); 
            pause(eps);
            if(tries>=99)
                throw(err);
            end
        end
    end
end

fprintf(['\n image ' save_folder image '_' identifier '_' initialGuessStr '_' method '_normal' num2str(normalize_bool) '_noi' num2str(noise) '_' mode '.tif' ' succesfully saved\n']);







end
end
end
end
end


toc

